<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="id" label="编号" width="100" sortable>
			</el-table-column>
			<el-table-column prop="name" label="店铺名称" width="100" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" sortable>
        <template scope="scope">
          <span v-if="scope.row.state == 1" style="color: green">启用</span>
          <span v-else-if="scope.row.state == -1" style="color: red">禁用</span>
          <span v-else-if="scope.row.state == 0" style="color: skyblue">待审核</span>
          <span v-else style="color: dimgrey">未知</span>
        </template>
			</el-table-column>
			<el-table-column prop="tel" label="电话" width="160" sortable>
			</el-table-column>
			<el-table-column prop="address" label="地址" width="160" sortable>
			</el-table-column>
			<el-table-column prop="logo" label="商标" width="100" sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="入驻时间" width="200" sortable>
			</el-table-column>
			<el-table-column prop="admin.id" label="管理员" width="100" sortable>
			</el-table-column>
			<el-table-column label="操作" fixed="right" width="180">
				<template scope="scope">
					<el-button type="success" size="small" @click="handleAudit(scope.$index, scope.row)">审核</el-button>
					<el-button type="danger" size="small" @click="handleForbidden(scope.row)">禁用</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next, sizes" background
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :page-sizes="pageSizes"
                     :total="total"
                     :current-page="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增/修改界面-->
		<!--<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="商铺名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="tel">
					<el-input v-model="saveForm.tel" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="saveForm.state">
						<el-radio class="radio" :label="1">启用</el-radio>
						<el-radio class="radio" :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
        <el-form-item label="地址">
          <el-input v-model="saveForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店招">
          <el-input v-model="saveForm.logo" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理员">
          <el-input v-model="saveForm.admin.id" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>-->

    <!--审核界面-->
    <el-dialog
        title="审核"
        :visible.sync="auditFormVisible"
        width="30%"
        :before-close="handleClose">
      <el-input
          type="textarea"
          :rows="3"
          placeholder="请输入驳回原因"
          v-model="textarea">
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="auditSuccess">审核通过</el-button>
        <el-button type="danger" @click="auditFalse">驳 回</el-button>
      </span>
    </el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
        title:"",
				filters: {
          keyword: ''
				},
				shops: [],
				total: 0,
				currentPage: 1,
        pageSize: 10,
        pageSizes: [5, 10, 15, 20],
				listLoading: false,
				sels: [],// 列表选中行
				saveFormVisible: false,// 新增界面是否显示
        auditFormVisible: false,// 审核界面是否显示
				saveLoading: false,
        textarea: '',
        id:null,
				saveFormRules: {
          name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					tel: [
						{ required: true, message: '请输入密码', trigger: 'blur' }
					]
				},
				// 新增界面数据
				shop: {}
			}
		},
		methods: {
		  // 驳回
      auditFalse(){
        this.auditFormVisible = false;
      },
      handleAdd(){},
      handleClose(){},
      // 审核通过
      auditSuccess(){
        this.auditFormVisible = false;
        let params = {
          id: this.id
        }
        this.$http.put("/shop/auditSuccess",params)
          .then(result => {
            let {success,message} = result.data;
            if(success){
              this.$message({
                message: "审核成功",
                type: 'success'
              });
              this.getShops();
            }else{
              this.$message({
                message: "审核失败",
                type: 'error'
              });
            }
          })
          .catch(result => {

          })
      },
      handleAudit(index,row){
        this.auditFormVisible = true;
        this.id = row.id;
      },
      handleForbidden(row){
        row.state = -1;
        this.$http.post("/shop/save",row)
          .then(result => {

          })
          .catch(result => {

          })
      },
      handleSizeChange(val){
        this.pageSize = val;
        this.getShops();
      },
      search(){
        this.currentPage = 1;
        this.getShops();
      },
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getShops();
			},
			// 获取商铺列表
			getShops() {
				let para = {
					currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.keyword
				};
				this.listLoading = true;
        this.$http.post("/shop/page",para)
          .then(result=>{
            this.listLoading = false;
            result = result.data;
            this.shops = result.rows;
            this.total = result.total;
          })
          .catch(result=>{
            console.log(result);
            this.$message({
              type: 'error',
              message: '系统错误,我们正在殴打程序员'
            })
          })
			},
			// 删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/shop/"+row.id)
            .then(result=>{
              this.listLoading = false;
              result = result.data;
              if (result.success){
                // 删除成功
                this.$message({
                  message: result.message,
                  type: 'success'
                });
                this.currentPage = 1;
                this.getShops();
              } else {
                // 删除失败
                this.$message({
                  message: result.message,
                  type: 'error'
                });
                this.currentPage = 1;
                this.getShops();
              }
            })
            .catch(result=>{
              this.listLoading = false;
              this.$message({
                type: 'error',
                message: '系统错误,我们正在殴打程序员'
              });
            })
				}).catch(() => {

				});
			},
			//新增
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {})
                .then(() => {
                  this.saveLoading = true; // 开启按钮忙等
							    let para = Object.assign({}, this.saveForm); // 克隆表单数据
                  // 处理后端需要的数据格式
                  this.$http.post("/shop/save",para)
                      .then(result=>{
                        this.saveLoading = false;
                        result = result.data;
                        if (result.success){
                          // 新增成功
                          this.$message({
                            message: result.message,
                            type: 'success'
                          });
                          this.currentPage = 1;
                          this.getShops();
                          this.$refs['saveForm'].resetFields(); // 重置表单校验
                          this.saveFormVisible = false;
                        } else {
                          // 新增失败
                          this.$message({
                            message: result.message,
                            type: 'error'
                          });
                          this.currentPage = 1;
                          this.getShops();
                        }
                      })
                      .catch(result=>{
                        this.saveLoading = false;
                        this.$message({
                          type: 'error',
                          message: '系统错误,我们正在殴打程序员'
                        });
                      })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)//.toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading = true;
				  // 发送Ajax请求删除数据
          this.$http.patch("/shop",ids)
              .then(result=>{
                this.listLoading = false;
                result = result.data;
                if (result.success){
                  // 删除成功
                  this.$message({
                    message: result.message,
                    type: 'success'
                  });
                  this.currentPage = 1;
                  this.getShops();
                } else {
                  // 删除失败
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                  this.currentPage = 1;
                  this.getShops();
                }
              })
              .catch(result=>{
                this.listLoading = false;
                this.$message({
                  type: 'error',
                  message: '系统错误,我们正在殴打程序员'
                });
              })
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getShops();
		}
	}

</script>

<style scoped>

</style>